<template>
  <div class="enterprise-login-container">
    <div class="login-wrapper">
      <!-- 左侧企业展示 -->
      <div class="banner-section">
        <div class="banner-content">
          <div class="logo-section">
            <div class="logo">
              <el-icon :size="36" color="#409eff">
                <OfficeBuilding />
              </el-icon>
              <span class="logo-text">OrangeHire</span>
            </div>
            <p class="subtitle">企业招聘管理平台</p>
          </div>

          <div class="illustration">
            <div class="enterprise-icon">
              <el-icon :size="100" color="#409eff">
                <Briefcase />
              </el-icon>
            </div>
            <h2 class="welcome-title">开启智能招聘</h2>
            <p class="welcome-desc">高效管理招聘流程，精准匹配优秀人才</p>
          </div>

          <div class="stats">
            <div class="stat-item">
              <div class="stat-number">10,000+</div>
              <div class="stat-label">企业用户</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">50,000+</div>
              <div class="stat-label">成功匹配</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">98%</div>
              <div class="stat-label">满意度</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录注册表单 -->
      <div class="form-section">
        <div class="form-container">
          <div class="form-tabs">
            <div class="tab-item" :class="{ active: activeTab === 'login' }" @click="activeTab = 'login'">
              登录
            </div>
            <div class="tab-item" :class="{ active: activeTab === 'register' }" @click="activeTab = 'register'">
              注册
            </div>
          </div>

          <!-- 登录表单 -->
          <div v-show="activeTab === 'login'" class="form-content">
            <div class="form-header">
              <h1>企业登录</h1>
              <p>登录您的企业账号，开始招聘之旅</p>
            </div>

            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="auth-form" size="large">
              <el-form-item prop="email">
                <el-input v-model="loginForm.email" placeholder="企业邮箱" prefix-icon="Message" clearable />
              </el-form-item>

              <el-form-item prop="password">
                <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="Lock" show-password
                  clearable />
              </el-form-item>

              <el-form-item>
                <div class="form-options">
                  <el-checkbox v-model="loginForm.remember">
                    记住我
                  </el-checkbox>
                  <el-link type="primary" @click="switchLoginType">
                    <el-icon>
                      <Switch />
                    </el-icon>
                    切换至超级管理员端
                  </el-link>
                  <el-link type="primary">
                    忘记密码？
                  </el-link>
                </div>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" class="auth-btn" :loading="loading" @click="handleLogin">
                  登录
                </el-button>
              </el-form-item>
            </el-form>
          </div>

          <!-- 注册表单 -->
          <div v-show="activeTab === 'register'" class="form-content">
            <div class="form-header">
              <h1>企业注册</h1>
              <p>创建企业账号，享受专业招聘服务</p>
            </div>

            <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" class="auth-form" size="large">
              <el-form-item prop="companyName">
                <el-input v-model="registerForm.companyName" placeholder="企业名称" prefix-icon="OfficeBuilding"
                  clearable />
              </el-form-item>

              <el-form-item prop="contactName">
                <el-input v-model="registerForm.contactName" placeholder="联系人姓名" prefix-icon="User" clearable />
              </el-form-item>

              <el-form-item prop="email">
                <el-input v-model="registerForm.email" placeholder="企业邮箱" prefix-icon="Message" clearable />
              </el-form-item>

              <el-form-item prop="password">
                <el-input v-model="registerForm.password" type="password" placeholder="设置密码" prefix-icon="Lock"
                  show-password clearable />
              </el-form-item>

              <el-form-item prop="confirmPassword">
                <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码" prefix-icon="Lock"
                  show-password clearable />
              </el-form-item>

              <el-form-item>
                <el-checkbox v-model="registerForm.agree">
                  我已阅读并同意
                  <el-link type="primary" :underline="false">《服务协议》</el-link>
                  和
                  <el-link type="primary" :underline="false">《隐私政策》</el-link>
                </el-checkbox>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" class="auth-btn" :loading="loading" @click="handleRegister">
                  注册
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { ElLoading, ElMessage, ElNotification } from 'element-plus'
import { OfficeBuilding, Briefcase } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const loginFormRef = ref()
const registerFormRef = ref()
const loading = ref(false)
const activeTab = ref('login')
const router = useRouter()
const loginForm = reactive({
  email: '',
  password: '',
  remember: false
})

const registerForm = reactive({
  companyName: '',
  contactName: '',
  email: '',
  password: '',
  confirmPassword: '',
  agree: false
})

const loginRules = {
  email: [
    { required: true, message: '请输入企业邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const registerRules = {
  companyName: [
    { required: true, message: '请输入企业名称', trigger: 'blur' }
  ],
  contactName: [
    { required: true, message: '请输入联系人姓名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入企业邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
      validator: (rule: unknown, value: string, callback: Function) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true

    // TODO: 调用登录API
    setTimeout(() => {
      loading.value = false
      ElMessage.success('登录成功')
      router.push('dashboard')
    }, 1000)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

const handleRegister = async () => {
  try {
    await registerFormRef.value.validate()
    if (!registerForm.agree) {
      ElMessage.warning('请先同意服务协议和隐私政策')
      return
    }

    loading.value = true

    // TODO: 调用注册API
    setTimeout(() => {
      loading.value = false
      ElMessage.success('注册成功')
      activeTab.value = 'login'
    }, 1000)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
const switchLoginType = async () => {
  // 1. 先显示加载动画
  const loadingInstance = ElLoading.service({
    fullscreen: true,
    text: '正在切换至超级管理员端...'
  });
  // 2. 等待 500ms（模拟加载过程）todo 这里需要做一些状态变更以及相关退出登录的网络请求;
  await new Promise(resolve => setTimeout(resolve, 1000));
  // 3. 关闭加载动画
  await nextTick(() => {
    loadingInstance.close();
  })
  // 4. 跳转到超级管理员端登录页
  router.push('/admin/login');
  ElNotification({
    title: 'Success',
    message: '已为您切换至超级管理员端',
    type: 'success',
  })
}
</script>

<style scoped>
.enterprise-login-container {
  min-height: 100vh;
  background: #f0f2f5;
  display: flex;
  align-items: center;
  justify-content: center;
  /* padding: 20px; */
}

.login-wrapper {
  width: 100%;
  min-width: 1200px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.12);
  overflow: hidden;
  display: flex;
  min-height: 100vh;
}

.banner-section {
  flex: 1;
  background: #409eff;
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: white;
  position: relative;
}

.banner-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><rect x="10" y="10" width="20" height="20" fill="%23ffffff" opacity="0.05"/><rect x="70" y="30" width="15" height="15" fill="%23ffffff" opacity="0.05"/><rect x="30" y="70" width="25" height="25" fill="%23ffffff" opacity="0.05"/></svg>');
}

.banner-content {
  position: relative;
  z-index: 1;
}

.logo {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.logo-text {
  font-size: 26px;
  font-weight: bold;
  margin-left: 12px;
  color: white;
}

.subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin: 0 0 40px 0;
}

.illustration {
  text-align: center;
  margin-bottom: 40px;
}

.enterprise-icon {
  margin-bottom: 20px;
}

.welcome-title {
  font-size: 24px;
  margin-bottom: 12px;
  font-weight: 600;
}

.welcome-desc {
  font-size: 16px;
  opacity: 0.9;
  line-height: 1.5;
  margin: 0;
}

.stats {
  display: flex;
  justify-content: space-between;
  gap: 20px;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  opacity: 0.8;
}

.form-section {
  flex: 1;
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  width: 100%;
  max-width: 400px;
}

.form-tabs {
  display: flex;
  margin-bottom: 30px;
  border-bottom: 2px solid #f0f0f0;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 12px 0;
  font-size: 16px;
  font-weight: 500;
  color: #909399;
  cursor: pointer;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
}

.tab-item.active {
  color: #409eff;
  border-bottom-color: #409eff;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h1 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 8px;
  font-weight: 600;
}

.form-header p {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.auth-form {
  margin-bottom: 20px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.auth-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
}

@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
    max-width: 400px;
  }

  .banner-section {
    padding: 40px 20px;
  }

  .form-section {
    padding: 40px 20px;
  }

  .stats {
    flex-direction: column;
    gap: 16px;
  }
}
</style>
